﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SVAP
{
    public partial class GetVideoForm : Form
    {
        //标志位
        bool isCatchingVideo;   //标志位，表示是否正在录像
        bool isOpened;          //标志位，表示是否已经打开视频设备

        int time;  //计时时间

        cVideo video;
        //VideoWork vw;

        public GetVideoForm()
        {
            InitializeComponent();

            lbl_time.Text = "";
            isCatchingVideo = false;  //初始化，此时没有录像
            timer_duration.Enabled = false;  //初始化，不打开计时器
            isOpened = false;        //初始化，此时没有打开视频设备

            video = new cVideo(pictureBox_video.Handle, pictureBox_video.Width, pictureBox_video.Height);
            //vw = new VideoWork(pictureBox_video.Handle, 0, 0, pictureBox_video.Width, pictureBox_video.Height);
        }

        //显示/关闭图像
        private void btn_moniter_Click(object sender, EventArgs e)
        {
            if (btn_moniter.Text == "显示图像" && isOpened == false)
            {
                try
                {
                    //cVideo video = new cVideo(pictureBox_video.Handle, pictureBox_video.Width, pictureBox_video.Height);
                    video.StartWebCam();
                    isOpened = true;  //置标志位为“打开”
                    btn_moniter.Text = "关闭图像";

                    //VideoWork vw = new VideoWork(pictureBox_video.Handle, 0, 0, pictureBox_video.Width, pictureBox_video.Height);
                    //vw.Start();
                }
                catch
                {
                    lbl_time.Text = "无法打开视频设备!";
                }
            }

            if (btn_moniter.Text == "关闭图像" && isOpened == true)
            {
                try
                {
                    video.CloseWebcam();
                    isOpened = false;  //置标志位为“打开”
                    btn_moniter.Text = "打开图像";
                }
                catch
                {
                    lbl_time.Text = "无法关闭视频设备!";
                }
            }
        }

        //录像控制
        private void btn_be_Click(object sender, EventArgs e)
        {
            if (btn_be.Text == "开始录像" && isOpened== true)
            {
                try
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "avi视频(*.avi)|*.avi";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        time = 0;                            //计时时间清零
                        timer_duration.Enabled = true;       //打开计时器
                        isCatchingVideo = true;              //置标志位，正在录像
                        btn_be.Text = "停止录像";            //修改按键显示，为"停止"
                        video.StarKinescope(sfd.FileName);   //开始录制视频，保存为avi格式
                    }
                }
                catch
                {
                    MessageBox.Show("无法录制视频!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            
            if(btn_be.Text == "停止录像" && isOpened== true)
            {
                try 
                {
                    video.StopKinescope();           //关闭视频录制
                    timer_duration.Enabled = false;  //关闭计时器
                    isCatchingVideo = false;         //修改标志位，不在录像
                    btn_be.Text = "开始录像";        //修改按键显示，为"开始"
                    isOpened = false;
                }
                catch
                {
                    MessageBox.Show("无法关闭视频录制。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        //图像抓拍
        private void btn_grabPic_Click(object sender, EventArgs e)
        {
            if (isOpened == true)
            {
                try
                {
                    SaveFileDialog sf = new SaveFileDialog();
                    sf.Filter = "bmp格式图片(*.bmp)|*.bmp";

                    if (sf.ShowDialog() == DialogResult.OK)
                    {
                        video.GrabImage(pictureBox_video.Handle, sf.FileName);
                    }
                }
                catch
                {
                    MessageBox.Show("无法抓拍图片!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            else
                MessageBox.Show("请先打开视频设备。");
        }

        //退出窗口
        private void btn_exit_Click(object sender, EventArgs e)
        {
            if (isCatchingVideo == true)
            {
                DialogResult dr = MessageBox.Show("正在录制视频，需要退出吗？", "注意", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                switch (dr)
                {
                    case DialogResult.Yes:
                        try
                        {
                            video.StopKinescope();           //关闭视频录制
                            //timer_duration.Enabled = false;  //关闭计时器
                            //isCatchingVideo = false;         //修改标志位，不在录像
                            this.Close();                    //关闭窗口
                        }
                        catch
                        {
                            MessageBox.Show("无法关闭视频录制。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;
                    case DialogResult.No:
                        break;
                }
            }
            else
                this.Close();
        }

        //定时器，显示用时
        private void timer_duration_Tick(object sender, EventArgs e)
        {
            time++;
            //lbl_time.Text += " " + (time / 60).ToString() + " 分 " + " " + (time % 60).ToString() + " 秒"; //显示已用时间
            lbl_time.Text = "正在录像...  已用时 " + (time / 60).ToString() + " 分 " + " " + (time % 60).ToString() + " 秒"; //显示已用时间;
        }
    }
}
